﻿Imports System.Windows.Forms

Public Class frmEditTable

    Private _FormType As enumFormType
    Private _GISShapeFile As DotSpatial.Data.FeatureSet

    Public Enum enumFormType
        Add
        Remove
        Rename
    End Enum


    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
        With _GISShapeFile.DataTable

            Select Case _FormType
                Case enumFormType.Add, enumFormType.Rename
                    If .Columns.Contains(txtNewName.Text) Then
                        WarningMsg("You must enter a unique field name.")
                        Exit Sub
                    End If
            End Select

            Select Case _FormType
                Case enumFormType.Add
                    Dim datatype As Type = Choose(cboFieldType.SelectedIndex + 1, GetType(String), GetType(Integer), GetType(Double))
                    .Columns.Add(txtNewName.Text, datatype)
                    If datatype Is GetType(String) Then
                        'CType(_GISShapeFile.DataTable.Columns(IIf(_FormType = enumFormType.Add, txtNewName.Text, cboFieldName.Text)), DotSpatial.Data.Field).Length = txtFieldSize.Value
                    End If
                Case enumFormType.Remove
                    .Columns.Remove(cboFieldName.Text)
                Case enumFormType.Rename
                    .Columns(cboFieldName.SelectedIndex).ColumnName = txtNewName.Text
            End Select

        End With

        _GISShapeFile.Save()

        Dim sel As Integer = cboFieldName.SelectedIndex
        FillColumns()
        cboFieldName.SelectedIndex = Math.Min(cboFieldName.Items.Count - 1, sel)

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmEditTable_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        FillColumns()

        Select Case _FormType
            Case enumFormType.Add
                Text = "Add Field"
                lblFieldName.Visible = False
                cboFieldName.Visible = False
                cboFieldType.SelectedIndex = 0
                txtFieldSize.Value = 254
                txtFieldSize.Enabled = False
            Case enumFormType.Remove
                Text = "Remove Field"
                lblNewName.Visible = False
                txtNewName.Visible = False
                lblFieldType.Enabled = False
                cboFieldType.Enabled = False
                lblFieldSize.Enabled = False
                txtFieldSize.Enabled = False
                cboFieldName.SelectedIndex = 0
            Case enumFormType.Rename
                Text = "Rename Field"
                lblFieldType.Enabled = False
                cboFieldType.Enabled = False
                lblFieldSize.Enabled = False
                txtFieldSize.Enabled = False
                cboFieldName.SelectedIndex = 0
        End Select

    End Sub

    Private Sub FillColumns()
        cboFieldName.Items.Clear()
        For Each col As DataColumn In _GISShapeFile.DataTable.Columns
            cboFieldName.Items.Add(col.ColumnName)
        Next
    End Sub

    Public Sub New(ByRef GISShapeFile As DotSpatial.Data.FeatureSet, FormType As enumFormType)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        _FormType = FormType
        _GISShapeFile = GISShapeFile

    End Sub

    Private Sub txtNewName_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtNewName.Validating
        txtNewName.Text = txtNewName.Text.Trim.Replace(" ", "_").ToUpper
        'note: control limits entry to 10 characters automatically
    End Sub

    Private Sub cboFieldName_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboFieldName.SelectedIndexChanged
        With _GISShapeFile.DataTable.Columns(cboFieldName.Text)
            Select Case .DataType
                Case GetType(String)
                    cboFieldType.SelectedIndex = 0
                    If TypeOf _GISShapeFile.DataTable.Columns(cboFieldName.Text) Is DotSpatial.Data.Field Then
                        txtFieldSize.Value = CType(_GISShapeFile.DataTable.Columns(cboFieldName.Text), DotSpatial.Data.Field).Length
                    Else
                        txtFieldSize.Value = 100
                    End If
                Case GetType(Integer), GetType(Int16)
                    cboFieldType.SelectedIndex = 1
                Case GetType(Double)
                    cboFieldType.SelectedIndex = 2
                Case Else
                    cboFieldType.SelectedIndex = -1
            End Select
        End With
        txtNewName.Text = "NEWFIELD"
    End Sub

    Private Sub cboFieldType_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboFieldType.SelectedIndexChanged
        lblFieldSize.Visible = cboFieldType.SelectedIndex = 0
        txtFieldSize.Visible = cboFieldType.SelectedIndex = 0
    End Sub
End Class
